package com.feimt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feimt.exce.CustomException;
import com.feimt.mapper.CategoryMapper;
import com.feimt.mapper.DishMapper;
import com.feimt.mapper.SetmealMapper;
import com.feimt.pojo.Category;
import com.feimt.pojo.Dish;
import com.feimt.pojo.Setmeal;
import com.feimt.service.CategoryService;
import com.feimt.service.DishService;
import com.feimt.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void remove1(Long id) {
        /*1.该分类是否有菜品*/
        /*SELECT COUNT(*) FROM dish WHERE category_id = '1'*/
        LambdaQueryWrapper<Dish> qwdish = new LambdaQueryWrapper<>();
        qwdish.eq(Dish::getCategoryId,id);
        Integer dishcount = dishMapper.selectCount(qwdish);
        if(dishcount > 0){
            log.info("分类下有菜品，不能删除");

            /*抛出异常*/
            throw new CustomException("分类下有菜品，不能删除");

        }

        /*2.该分类是否有套餐*/
        /*SELECT COUNT(*) FROM setmeal WHERE category_id = '1'*/
        LambdaQueryWrapper<Setmeal> qwSetmeal = new LambdaQueryWrapper<>();
        qwSetmeal.eq(Setmeal::getCategoryId,id);
        Integer setmealcount = setmealMapper.selectCount(qwSetmeal);
        if(setmealcount > 0 ){
            log.info("分类下有套餐，不能删除");
            /*抛出异常*/
            throw new CustomException("分类下有套餐，不能删除");

        }

        /*删除*/
        this.removeById(id);
    }
}
